﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.RightsManagement;
using System.Text;
using System.Threading.Tasks;
using Assisticant.Fields;
using UGD.Models;

namespace UGD.ViewModels
{
    public class MainViewModel
    {
        private readonly Document _document;
        public IDialogService _dialogService;
        private Observable<bool> _isBusy = new Observable<bool>(false);

        public MainViewModel(Document document, IDialogService dialogService)
        {
            _document = document;

            _dialogService = dialogService;
            _document.Error = "Hello ";
        }


        public IEnumerable<ItemViewModel> Items
            => from item in _document.Items select new ItemViewModel(item, TotalTicksInRange);

        public DateTime RangeStart => _document.RangeStart;
        public DateTime RangeEnd => _document.RangeEnd;

        public ItemViewModel ItemHeader => new ItemViewModel(_document.ItemHeader, TotalTicksInRange);


        public TimeSpan TimeSpan
        {
            get
            {
                return _document.TimeSpan;
            }
            set
            {
                _document.TimeSpan = value;
            }
        }
        public int Pixels
        {
            get
            {
                return _document.Pixels;
            }
            set
            {
                _document.Pixels = value;
            }
        }

        public string ErrorMessage
        {
            get
            {
                return _document.Error;
            }
            set
            {
                _document.Error = value;
            }
        }

        public decimal TotalTicksInRange => _document.RangeEnd.Ticks - _document.RangeStart.Ticks;

        public async void Load()
        {
            try
            {
                _isBusy.Value = true;
                _document.Error = "";
                _document.FileName = await this._dialogService.AskForInputAsync("Import Data", "Enter File Path");
               _document.LoadDataFile();
            }
            catch (Exception ex) {
                _document.Error = ex.Message;

            }
            finally { _isBusy.Value = false; }




        }

        public bool IsBusy
        {
            get
            {
                return _isBusy;
            }
            set
            {
                _isBusy.Value = value;
            }

        }

    }
}
